#! /bin/bash

LIB=$1
OLIB=$2
PWD=`pwd`
LIBSDL=/usr/lib/libSDL.so
LIBSDLGFX=/usr/lib/libSDL_gfx.so
LIBSDLIMG=/usr/lib/libSDL_image.so
LIBGL=/usr/lib/libGL.so
LIBGLU=/usr/lib/libGLU.so
LIBGLUT=/usr/lib/libglut.so
LIBGLEW=/usr/lib/libGLEW.so

if [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" != "--install" ]; then
	echo "This is MGPI Instalation Script. DO NOT use it. Use sudo ./mgpiinstall instead"
	exit 1
fi

echo "Checking dependecies..."
if [ ! -f $LIBSDL ]; then
	echo "Instalation failed. No $LIBSDL found."
	exit 1
else echo "$LIBSDL.........checked"
fi
if [ ! -f $LIBSDLGFX ]; then
	echo "Instalation failed. No $LIBSDLGFX found."
	exit 1
else echo "$LIBSDLGFX.........checked"
fi
if [ ! -f $LIBSDLIMG ]; then
	echo "Instalation failed. No $LIBSDLIMG found."
	exit 1
else echo "$LIBSDLIMG.........checked"
fi
if [ ! -f $LIBGL ]; then
	echo "Instalation failed. No $LIBGL found."
	exit 1
else echo "$LIBGL.........checked"
fi
if [ ! -f $LIBGLU ]; then
	echo "Instalation failed. No $LIBGLU found."
	exit 1
else echo "$LIBGLU.........checked"
fi
if [ ! -f $LIBGLUT ]; then
	echo "Instalation failed. No $LIBGLUT found."
	exit 1
else echo "$LIBGLUT.........checked"
fi
if [ ! -f $LIBGLEW ]; then
	echo "Instalation failed. No $LIBGLEW found."
	exit 1
else echo "$LIBGLEW.........checked"
fi

echo "Instaling header files..."
if [ -d /usr/include/MGPI ]; then
	if [ -e /usr/include/MGPI/mgpi.h ]; then
		sudo rm -f /usr/include/MGPI/mgpi.h
	fi
else
	sudo mkdir /usr/include/MGPI
fi
sudo sh inst > /usr/include/MGPI/mgpi.h

echo "Instaling library files..."
if [ -e /usr/lib/$LIB ]; then
	sudo rm -f /usr/lib/$LIB
fi
sudo ln -s $PWD/$OLIB /usr/lib/$LIB

if [ -e /usr/lib/libMGPI.so ]; then
	sudo rm -f /usr/lib/libMGPI.so
fi
sudo ln -s /usr/lib/$LIB /usr/lib/libMGPI.so

if [ -e /usr/lib/libMGPF.so ]; then
	sudo rm -f /usr/lib/libMGPF.so
fi
sudo ln -s /usr/lib/$LIB /usr/lib/libMGPF.so

if [ -e /usr/lib/libMGPI.a ]; then
	sudo rm -f /usr/lib/libMGPI.a
fi
sudo ar cr /usr/lib/libMGPI.a /usr/lib/$LIB

if [ -e /usr/lib/liMGPF.a ]; then
    sudo rm -f /usr/lib/libMGPF.a
fi
sudo ar cr /usr/lib/libMGPF.a /usr/lib/$LIB
echo "Instalation complited.\nInsert \"#include <MGPI/mgpi.h>\" and link /usr/lib/$LIB to Your MGPI application."
